<?php
namespace app\model;

/**
 * 核心Db框架
 * @author sunhaoqiang(shqdev@163.com)
 * @time: 2019-08-25 16:38
*/

class Base
{
    private $pdo;
    public $table;
    public function __construct()
    {
        $this->DB();
        // $this->table = $table;
    }
    private function DB()
    {
        $dsn = "mysql:host=127.0.0.1;dbname=spider;port=3306;charset=utf8";
        $user = 'root';
        $pass = 'root';
        $this->pdo = new \PDO($dsn,$user,$pass);
    }
    public function setTable($table)
    {
        $this->table = $table;
        return $this;
    }

    public function arr2str($data)
    {
        $fields = implode(',', array_keys($data));
        $value = implode("','", array_values($data));  
        $value = "('".$value."')";
        return $value;
    }
    public function insertAll($data)
    {
        // var_dump($data);die;
        $fields = implode(',', array_keys($data[0]));
        $data_str = array_map([$this, 'arr2str'], $data);

        $data = implode(',', $data_str);

        $sql = "INSERT INTO ".$this->table." ($fields) VALUES $data";

        // var_dump($sql);die;
        $res = $this->pdo->exec($sql);
        if (!$res) {
            return $this->pdo->errorInfo();
        }
        return true;
        // var_dump($res, $this->pdo->errorInfo());die;
    }
    
}